Option Explicit

'################################################################
' Field
'################################################################

Private fieldLogicName As String
Private fieldPhysicalName As String
Private fieldTypeName As String
Private fieldMainSize As Integer
Private fieldSubSize As Integer ' use in numbric
Private fieldIsPk As Boolean

Private parentTable As DBTableDefine
'################################################################
' Constructor
'################################################################
Private Sub Class_Initialize()
End Sub

Private Sub Class_Terminate()
End Sub

'################################################################
' Init
'################################################################
' 初始化当前的列定义

' @param logicNameVal 列的逻辑名
' @param physicalNameVal 列的物理名
' @param typeVal 列的类型
' @param sizeVal 列的长度
' @param pkVal 列是否是主键
Public Sub Init(ByRef logicNameVal As String, _
                ByRef physicalNameVal As String, _
                ByRef typeVal As String, _
                ByRef sizeVal As String, _
                ByRef pkVal As String)
    Me.LogicName = logicNameVal
    Me.PhysicalName = physicalNameVal
    Me.TypeName = typeVal
    Me.setMainSize sizeVal
    Me.IsPk = pkVal
End Sub

'################################################################
' Property
'################################################################
' 获取当前列定义的名称
' 如果有物理名，则有些返回物理名。如果没有物理名，则返回逻辑名
Public Property Get Name() As String
    If fieldPhysicalName = "" Then
        Name = fieldLogicName
    Else
        Name = fieldPhysicalName
    End If
End Property

' 获取当前列定义的逻辑名
Public Property Get LogicName() As String
    LogicName = fieldLogicName
End Property

' 设置当前列定义的逻辑名
Public Property Let LogicName(ByVal val As String)
    fieldLogicName = val
End Property

' 获取当前列定义的物理名
Public Property Get PhysicalName() As String
    PhysicalName = fieldPhysicalName
End Property

' 设置当前列定义的物理名
Public Property Let PhysicalName(ByVal val As String)
    fieldPhysicalName = val
End Property

' 获取当前列定义的类型
Public Property Get TypeName() As String
    TypeName = fieldTypeName
End Property

' 获取当前列定义的类型
' 如果参数 val 是空字符串，则默认使用字符串类型
Public Property Let TypeName(ByVal val As String)
    fieldTypeName = UtilsDBTable.analyzeTypeName(val)
End Property

' 获取当前列定义的长度
Public Property Get MainSize() As Long
    MainSize = fieldMainSize
End Property

' 获取当前列定义的小数长度
Public Property Get SubSize() As Integer
    SubSize = fieldSubSize
End Property

' 获取当前列是否是主键
Public Property Get IsPk() As Boolean
    IsPk = fieldIsPk
End Property

' 设置当前列是否是主键
Public Property Let IsPk(ByVal val As Boolean)
    fieldIsPk = val
End Property

' 检查当前类是否是整数
Public Property Get IsInteger() As Boolean
    IsInteger = (TypeName = UtilsDBTable.TYPE_INTEGER)
End Property

' 检查当前类是否是小数
Public Property Get IsDecimal() As Boolean
    IsDecimal = (TypeName = UtilsDBTable.TYPE_DECIMAL)
End Property

' 设置当前列定义所在的表定义
Public Property Let Parent(ByRef p As DBTableDefine)
    Set parentTable = p
End Property

' 获取当前列定义所在的表定义
Public Property Get Parent() As DBTableDefine
    Set Parent = parentTable
End Property

'################################################################
' Function
'################################################################
' 设置列的长度
Public Function setMainSize(ByVal val As String)
    If InStr(val, ",") <> 0 Then
        ' 如果参数是用逗号分隔的两个数字，则当作小数，并设置 subSize
        val = Replace(val, " ", "")
        Dim splitArray() As String
        splitArray = Split(val, ",")
        fieldMainSize = UtilsNumber.str2Num(splitArray(0))
        fieldSubSize = UtilsNumber.str2Num(splitArray(1))

        ' 如果小数部分的长度是 0，则将类型设置为整数
        If fieldSubSize <= 0 Then
            fieldSubSize = -1
            fieldTypeName = UtilsDBTable.TYPE_INTEGER
        Else
            fieldTypeName = UtilsDBTable.TYPE_DECIMAL
        End If
    Else
        fieldMainSize = UtilsNumber.str2Num(val)
        fieldSubSize = -1
    End If
End Function
